﻿Imports Microsoft.VisualBasic
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO

'Original Project: http://rexor.codeplex.com/
'Thanks, Todd, for this algorithm

Namespace Criptografia

    Public Class Rexor

        Public Function CriptografaDescriptografa(ByVal Texto As String, ByVal Chave As String) As String
            Try
                Dim x As String
                If Right(Texto, 1) = "=" Then
                    Chave = ToHex(Chave)
                    Texto = Texto.Remove(Texto.Length - 1)
                    x = xorstring(Reverse(xorstring(Texto, Chave)), Chave)
                    x = HexToString(x)
                Else
                    Chave = ToHex(Chave)
                    Texto = ToHex(Texto)
                    x = xorstring(Reverse(xorstring(Texto, Chave)), Chave)
                    x = x + "="
                End If
                Return x
            Catch
                MsgBox("Incorrect Password")
            End Try
        End Function

        Private Function xorstring(ByVal input As String, ByVal key As String) As String
            Dim Result1 As String
            key = Hashkey(key)
            Dim a As Array = input.ToCharArray
            Dim k As Array = key.ToCharArray
            Dim q As Integer = 0
            For i = 0 To a.Length - 1
                If q = k.Length - 1 Then
                    q = 0
                End If
                Result1 = Result1 + Chr((Asc(k(q))) Xor Asc(a(i)))
                q = q + 1
            Next
            Return Result1
        End Function

#Region "Helper Functions"

        Private Function Hashkey(ByVal key As String)
            Dim sha1CryptoService As New SHA1CryptoServiceProvider()
            Dim byteValue = Encoding.UTF8.GetBytes(key)
            Dim hashValue = sha1CryptoService.ComputeHash(byteValue)
            Return Convert.ToBase64String(hashValue)
        End Function

        Private Function ToHex(ByVal input As String) As String
            Dim str As String = input
            Dim byteArray() As Byte
            Dim hexNumbers As System.Text.StringBuilder = New System.Text.StringBuilder
            byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)
            For i As Integer = 0 To byteArray.Length - 1
                hexNumbers.Append(byteArray(i).ToString("x"))
            Next
            Return hexNumbers.ToString()
        End Function

        Private Function HexToString(ByVal hex As String) As String
            Dim text As New System.Text.StringBuilder(hex.Length \ 2)
            For i As Integer = 0 To hex.Length - 2 Step 2
                text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
            Next
            Return text.ToString
        End Function

        Private Function Reverse(ByVal input As String)
            Dim rev() As Char = input.ToCharArray
            Array.Reverse(rev)
            Return CStr(rev)
        End Function

#End Region

    End Class

End Namespace